package com.dawn.cloud.pos.drive.utils;

/**
 * Created by yyh on 2018/4/12.
 */

public class StringEnCodeTools {

    private final static char[] HEXDIAL =  {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public StringEnCodeTools() {
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if(src != null && src.length > 0) {
            for(int i = 0; i < src.length; ++i) {
                int v = src[i] & 255;
                String hv = Integer.toHexString(v);
                if(hv.length() < 2) {
                    stringBuilder.append(0);
                }

                stringBuilder.append(hv);
            }

            return stringBuilder.toString();
        } else {
            return null;
        }
    }

    public static byte[] hexStringToBytes(String hexString) {
        if(hexString != null && !hexString.equals("")) {
            hexString = hexString.toUpperCase();
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] d = new byte[length];

            for(int i = 0; i < length; ++i) {
                int pos = i * 2;
                d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }

            return d;
        } else {
            return null;
        }
    }

    private static byte charToByte(char c) {
        return (byte)"0123456789ABCDEF".indexOf(c);
    }

    public static void main(String[] args) {
        String s = "43";
        byte[] bcd = ASCII_To_BCD(s.getBytes(), s.length());
        String s1 = bcd2Str(bcd);
        System.out.println(s1);
    }

    private static byte asc_to_bcd(byte asc) {
        byte bcd;
        if(asc >= 48 && asc <= 57) {
            bcd = (byte)(asc - 48);
        } else if(asc >= 65 && asc <= 70) {
            bcd = (byte)(asc - 65 + 10);
        } else if(asc >= 97 && asc <= 102) {
            bcd = (byte)(asc - 97 + 10);
        } else {
            bcd = (byte)(asc - 48);
        }

        return bcd;
    }

    public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) {
        byte[] bcd = new byte[asc_len / 2];
        int j = 0;

        for(int i = 0; i < (asc_len + 1) / 2; ++i) {
            bcd[i] = asc_to_bcd(ascii[j++]);
            bcd[i] = (byte)((j >= asc_len?0:asc_to_bcd(ascii[j++])) + (bcd[i] << 4));
        }

        return bcd;
    }

    public static String bcd2Str(byte[] bytes) {
        char[] temp = new char[bytes.length * 2];

        for(int i = 0; i < bytes.length; ++i) {
            char val = (char)((bytes[i] & 240) >> 4 & 15);
            temp[i * 2] = (char)(val > 9?val + 65 - 10:val + 48);
            val = (char)(bytes[i] & 15);
            temp[i * 2 + 1] = (char)(val > 9?val + 65 - 10:val + 48);
        }

        return new String(temp);
    }

    public static String byte2String (byte[] buffer) {
        StringBuilder ret = new StringBuilder("");
        for (int i = 0;i < buffer.length;i++) {
            int temp = (int)buffer[i];
            ret.append(HEXDIAL[((temp >> 4) & 0x0F)]).append(HEXDIAL[(temp & 0x0F)]);
        }

        return ret.toString();
    }
}
